<template>
  <!-- 飞行数据统计详情 -->
  <div class="flight">
    <div class="flight-top">
      <div class="flight-top-left">
        <p>飞行数据统计</p>
        <p>全部飞行里程和全部飞行面积自2022年2月20日起开始统计</p>
      </div>
      <div class="flight-top-right">
        <div class="flight-top-right-item" v-for="(item, index) in flyData" :key="index">
          <img class="flight-top-right-item-img" :src="item.src" />
          <div class="flight-top-right-item-msg">
            <p>{{ item.title }}</p>
            <p>
              <span>{{ item.num }}</span>
              {{ item.unit }}
            </p>
          </div>
        </div>
      </div>
    </div>
    <div class="flight-con">
      <div class="flight-con-top">
        <el-form size="small" :inline="true" :model="formQuery.form" label-width="118px">
          <el-form-item label="设备序列号">
            <el-input v-model.trim="formQuery.form.serial_no" placeholder="请输入" maxLength="40" clearable></el-input>
          </el-form-item>
          <el-form-item label="设备品牌" class="filter">
            <el-select v-model="formQuery.form.brand_name" clearable>
              <el-option
                v-for="(item, index) in formQuery.brandList"
                :key="index"
                :label="item.name"
                :value="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属单位" class="filter">
            <el-select v-model="formQuery.form.unit_name" clearable>
              <el-option
                v-for="item in formQuery.companyList"
                :key="item.id"
                :label="item.unit_name"
                :value="item.unit_name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="起止时间">
            <el-date-picker
              v-model="formQuery.time"
              type="daterange"
              range-separator="→"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              @change="changeDate"
            />
          </el-form-item>
        </el-form>
        <div class="flight-con-top-search">
          <el-button class="search-btn-res" @click="formQuery.clickReset">重置</el-button>
          <el-button class="search-btn-sear" type="primary" @click="formQuery.clickSearch()">查询</el-button>
        </div>
      </div>
      <div class="flight-con-table">
        <comTable
          :loading="pagination.loading"
          :tableData="tableData"
          :listCount="pagination.total"
          :pageSize="pagination.pageSize"
          :currentPage="pagination.currentPage"
          :userColumn="tableColumns()"
          @updatePage="handleCurrentChange"
          @sizePage="handleSizeChange"
        >
          <template #serial_no>
            <el-table-column label="设备序列号" minWidth="120">
              <template #default="scope">
                <span style="color: #1182fb; padding: 0 10px">{{ scope.row.serial_no }}</span>
              </template>
            </el-table-column>
          </template>
        </comTable>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import {ref, reactive, onMounted} from 'vue';
import manageApi from '@/api/manage.ts';
import registerApi from '@/api/register.ts';
import censusApi from '@/api/census.ts';
import datetransform from '@/utils/index.ts';
import comTable from '@/components/comTable/index.vue';
import iconArea from '@/assets/img/census/icon_area.png';
import iconDevice from '@/assets/img/census/icon_device.png';
import iconMileage from '@/assets/img/census/icon_mileage.png';

// 飞行数据
const flyData = ref([
  {
    src: iconDevice,
    title: '全部设备',
    num: 1876,
    unit: '架',
  },
  {
    src: iconMileage,
    title: '全部飞行里程',
    num: 1876,
    unit: 'km',
  },
  {
    src: iconArea,
    title: '全部飞行面积',
    num: 1876,
    unit: 'km²',
  },
]);
// 顶部查询栏相关
const formQuery = reactive({
  form: {},
  clickSearch: () => {
    pagination.currentPage = 1;
    getTableData();
  },
  clickReset: () => {
    formQuery.form = {
      serial_no: '',
      brand_name: '',
      unit_name: '',
      created_at_from: '',
      created_at_to: '',
    };
    formQuery.time = [];
    pagination.currentPage = 1;
    getTableData();
  },
  brandList: [],
  companyList: [],
  time: [],
});
// 选择时间
const changeDate = () => {
  formQuery.time = [datetransform.formYYMMDD(formQuery.time[0]), datetransform.formYYMMDD(formQuery.time[1])];
  [formQuery.form.created_at_from, formQuery.form.created_at_to] = formQuery.time;
};
// 表格数据
const tableData = ref([]);
// 分页相关
const pagination = reactive({
  loading: false,
  total: 15,
  pageSize: 15,
  currentPage: 1,
});
const handleSizeChange = (e: any) => {
  pagination.pageSize = e.pageSize;
  getTableData();
};
const handleCurrentChange = (e: any) => {
  pagination.currentPage = e.currentPage;
  getTableData();
};
// 获取单位列表
const getCompanyList = () => {
  registerApi
    .getCompanyList()
    .then((res) => {
      formQuery.companyList = res.data;
    })
    .catch((err) => {
      throw err;
    });
};
// 获取设备品牌列表
const getBrandList = () => {
  manageApi
    .deviceBrandList()
    .then((res) => {
      formQuery.brandList = res.data;
    })
    .catch((err) => {
      throw err;
    });
};
// 获取任务总和
const getTaskSum = () => {
  censusApi
    .taskSum({type: 'all'})
    .then((res) => {
      flyData.value[0].num = res.data.all.count;
      flyData.value[1].num = res.data.all.meter.toFixed(2);
      flyData.value[2].num = res.data.all.area.toFixed(2);
    })
    .catch((err) => {
      throw err;
    });
};
// 获取表格数据
const getTableData = () => {
  pagination.loading = true;
  censusApi
    .getFlightData({per_page: pagination.pageSize, page: pagination.currentPage, ...formQuery.form})
    .then((res) => {
      if (res.status === 0) {
        tableData.value = res.data.data.map((item) => {
          item.created_at = formatDate(item.created_at);
          return item;
        });
        pagination.total = res.data.total;
        pagination.pageSize = Number(res.data.per_page);
        pagination.currentPage = Number(res.data.page);
        pagination.loading = false;
      }
    })
    .catch((err) => {
      throw err;
    });
};
// 时间转换
const formatDate = (time) => {
  const date = new Date(time);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  const theDay = `${year.toString()}-${month.toString()}-${day.toString()} ${hour.toString()}:${minute.toString()}:${second.toString()}`;
  return theDay;
};

onMounted(() => {
  getTableData();
  getBrandList();
  getCompanyList();
  getTaskSum();
});
// 表格表头
const tableColumns = () => [
  {
    prop: 'serial_no',
    label: '设备序列号',
    slot: 'serial_no',
  },
  {
    prop: 'contact_user',
    label: '平台用户名',
    minWidth: 100,
  },
  {
    prop: 'contact_mobile',
    label: '账号',
    minWidth: 100,
  },
  {
    prop: 'feima_mobile',
    label: '飞行员账号',
    minWidth: 100,
  },
  {
    prop: 'unit_name',
    label: '所属单位',
    minWidth: 160,
  },
  {
    prop: 'created_at',
    label: '飞行时间',
  },
  {
    prop: 'brand_name',
    label: '设备品牌',
    minWidth: 120,
  },
  {
    prop: 'area',
    label: '飞行面积（km²）',
    minWidth: 120,
  },
  {
    prop: 'meter',
    label: '飞行里程（km）',
    minWidth: 120,
  },
];
</script>
<style lang="scss" scoped>
.flight {
  width: 100%;
  height: 100%;
  background: #f1f1f1;
  padding: 15px 30px;
  box-sizing: border-box;
  &-top {
    height: 111px;
    background: #ffffff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 41px;
    &-left {
      p {
        font-size: 24px;
        font-weight: bold;
        color: #333333;
        margin: 0;
        &:nth-child(2) {
          font-size: 13px;
          color: #999999;
          margin-top: 11px;
        }
      }
    }
    &-right {
      display: flex;
      align-items: center;
      &-item {
        margin-right: 73px;
        display: flex;
        align-items: center;
        &-img {
          width: 60px;
          height: 60px;
        }
        &-msg {
          margin-left: 19px;
          p {
            color: #999999;
            margin: 0;
            font-size: 14px;
            > span {
              font-weight: bold;
              font-size: 32px;
              color: #5ac289;
            }
          }
        }
        &:nth-child(2) {
          span {
            color: #00d5d7;
          }
        }
        &:nth-child(3) {
          span {
            color: #2d97ff;
          }
        }
      }
    }
  }
  &-con {
    margin-top: 21px;
    height: 85%;
    background: #ffffff;
    padding: 15px 30px;
    box-sizing: border-box;
    &-top {
      height: 145px;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    &-table {
      height: 84%;
      .new-table {
        height: 100% !important;
        .el-table {
          height: 80%;
        }
      }
    }
  }
}
</style>
